package com.heaven.spring;

import com.heaven.service.remote.impl.TestServiceImpl;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.context.annotation.*;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.beans.PropertyVetoException;

@Configuration
@ComponentScan("com.heaven")
@ImportResource
@PropertySource("classpath:config.properties")
public class BaseSpringConfig {
    @Resource
    private Environment env;
    @Bean(destroyMethod = "close")
    public DataSource dataSource(){
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        try {
            dataSource.setDriverClass(env.getProperty("mysql_driverClassName"));
            dataSource.setJdbcUrl(env.getProperty("mysql_url"));
            dataSource.setUser(env.getProperty("mysql_username"));
            dataSource.setPassword(env.getProperty("mysql_password"));
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
        return dataSource;
    }
    @Bean
    public JdbcTemplate jdbcTemplate(){
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        jdbcTemplate.setDataSource(this.dataSource());
        return jdbcTemplate;
    }
}
